<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-07 19:35:27
 * @LastEditTime: 2022-08-24 14:58:13
 * @LastEditors: CHINAKAIYUAN
 * @Description: 
 */

namespace App\Http\Properties;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection;
use ReflectionClass;
use ReflectionProperty;

class Properties
{
    public static function make($modelData)
    {
        $class = get_called_class();

        return (new Collection(self::filterPropertiy($modelData, $class)))
            ->map(function ($val, $key) use ($class) {
                //是二维数组，并且存在对应的key方法
                if (getArrayableItems($val) !== false && method_exists($class, $key)) {
                    //过滤子属性
                    return (self::filterPropertiy($val, $class::{$key}()));
                } else
                    return $val;
            });
    }
    //更新属性，过滤字段
    public static function filterPropertiy($array, $class)
    {
        return (new Collection($array))->filter(function ($val, $key) use ($class) {
            return property_exists($class, $key);
        });
    }
    public function __call($name, $arguments)
    {
        return self::{$name}(...$arguments);
    }
}
